const MyPromise = require('./my-promise.js');

/*const p = new MyPromise((resolve, reject) => {
  // setTimeout(() => {
  // throw new Error('exector error');
  resolve('success');
  // reject('fail');
  // }, 1000);

}).then(v => {
  console.log(v);
  return 1;
})
.then(v => {
  const o = other();
  console.log('------------', v);
  return o;
}, e => {
  console.log( '-----cb', e);
  return 'cb'
})
.catch(e => {
  console.log('-----inCatch ', e);
  return 3;
})
.then(v => {
  console.log(v);
  // throw 'error';
  return 'hello  then'
}).finally(() => {
  console.log('----finally');
}).catch((e) => {
  console.log(e);
}).then(v => {
  console.log(v);
})*/

function getPromise(value, isErr, timout, isOrigin) {
  const P = isOrigin ? Promise : MyPromise;
  return new P((resolve, reject) => {
    if (timout) {
      setTimeout(() => {
        isErr ? reject(value) : resolve(value);
      }, timout);
    } else {
      isErr ? reject(value) : resolve(value);
    }
  });
}

// const p1 = getPromise('p1-----', false, 1000);
// const p2 = getPromise('p2-----', false, 100);
/*
const p1 = getPromise('err1-----', true, 1000);
const p2 = getPromise('p2-----', false, 2000);
MyPromise.all([p1, 23, 43, p2]).then(values => {
  console.log(values);
}, reason => {
  console.log(reason);
});
*/

const p1 = getPromise('p1-----', true, 1000);
const p2 = getPromise('p2-----', false, 2000);
// Promise.allSettled([p1, p2]).then(values => {
//   console.log(values);
// }, (r) => {
//   console.log(r, '-----r-----')
// });
// MyPromise.any([p1, p2])
MyPromise.race([p1, p2])
  .then(value => {
  console.log(value);
}, reason => {
  console.log(reason, 'err-------');
});
// const p1 = getPromise('err1-----', true, 1000);
// const p2 = getPromise('p2-----', false, 2000);
// MyPromise.allSettled([p1, p2]).then(values => {
//   console.log(values);
// });

function other() {
  return new MyPromise((resolve, reject) => {
    setTimeout(() => {
      resolve('other');
    }, 1000);
  })
}
/*
new Promise((resolve, reject) => {
  resolve('rejecsldkjfkdsl');
}).then(v => {
  console.log(v);
  return 1;
}).then(v => {
  console.log(v);
  throw 'error'
  return 2;
}).finally(() => {
  console.log('---finaly');
}).catch(e => {
  console.log(e);
})*/
